package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"tizhi_front/nspel"
)

type ArticleLike struct {
	Id        int64 `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	StudentId int64 `gorm:"column:student_id"`
	ArticleID int64 `gorm:"column:article_id"`
}

func (*ArticleLike) TableName() string {
	return "article_like"
}

type ArticleLikeE struct{}

func (a *ArticleLikeE) SelectByStudentIdArticleId(studentId int64, articleId int64) (err error) {
	var articleLike ArticleLike0
	key := "article_like0.student_id:" + strconv.FormatInt(studentId, 10) + ".article_id:" + strconv.FormatInt(articleId, 10)
	err = nspel.RedisGetGob(key, &articleLike)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("article_id = ?", articleId).First(&articleLike).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, articleLike)
	return
}

func (a *ArticleLikeE) SelectCountByArticleId(articleId int64) (count int64, err error) {
	key := "article_like.article_id:" + strconv.FormatInt(articleId, 10) + ".count"
	err = nspel.RedisGetInt64(key, &count)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Table("article_like").Where("article_id = ?", articleId).Count(&count).Error; err != nil {
		return
	}
	nspel.RedisSetInt64(key, count)
	return
}

type ArticleLike0 struct {
	Id int64 `gorm:"primary_key;column:id"`
}

func (*ArticleLike0) TableName() string {
	return "article_like"
}

func (a *ArticleLike0) SelectByStudentIdArticleId(studentId int64, articleId int64) (err error) {
	key := "article_like0.student_id:" + strconv.FormatInt(studentId, 10) + ".article_id:" + strconv.FormatInt(articleId, 10)
	err = nspel.RedisGetGob(key, a)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("article_id = ?", articleId).First(a).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, a)
	return
}

func (a *ArticleLike0) Delete(studentId int64, articleId int64) (err error) {
	tx := nspel.GetOrm().Begin()
	err = tx.Delete(a).Error
	if err != nil {
		tx.Rollback()
		return
	}
	err = tx.Exec("UPDATE article SET like_count = like_count - 1 WHERE id = ?", articleId).Error
	if err != nil {
		tx.Rollback()
		return
	}
	article := Article2{}
	err = article.SelectById(articleId)
	if err != nil {
		tx.Rollback()
		return
	}
	tx.Commit()
	_ = nspel.RedisKeysDelete("article_like0.student_id:"+strconv.FormatInt(studentId, 10)+".article_id:"+strconv.FormatInt(articleId, 10), "article_like.article_id:"+strconv.FormatInt(articleId, 10)+".count", "article0.type:"+strconv.Itoa(int(article.Type))+".update_at.desc.all", "article0.type:"+strconv.Itoa(int(article.Type))+".watch_count.desc.all", "article1.id:"+strconv.FormatInt(articleId, 10), "article3.id:"+strconv.FormatInt(articleId, 10))
	return
}

type ArticleLike1 struct {
	StudentId int64 `gorm:"column:student_id"`
	ArticleId int64 `gorm:"column:article_id"`
}

func (*ArticleLike1) TableName() string {
	return "article_like"
}

func (a *ArticleLike1) Insert() (err error) {
	tx := nspel.GetOrm().Begin()
	err = tx.Create(a).Error
	if err != nil {
		tx.Rollback()
		return
	}
	_ = nspel.RedisKeysDelete("article_like.article_id:" + strconv.FormatInt(a.ArticleId, 10) + ".count")
	err = tx.Exec("UPDATE article SET like_count = like_count + 1 WHERE id = ?", a.ArticleId).Error
	if err != nil {
		tx.Rollback()
		return
	}
	article := Article2{}
	err = article.SelectById(a.ArticleId)
	if err != nil {
		tx.Rollback()
		return
	}
	_ = nspel.RedisKeysDelete("article0.type:"+strconv.Itoa(int(article.Type))+".update_at.desc.all", "article0.type:"+strconv.Itoa(int(article.Type))+".watch_count.desc.all", "article1.id:"+strconv.FormatInt(a.ArticleId, 10), "article3.id:"+strconv.FormatInt(a.ArticleId, 10))
	tx.Commit()
	return
}
